package com.wyp168.leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName IsHappySolution
 * @Title TODO
 * @Description TODO
 * @Author wyp168
 * @Date 2022/10/11 20:30
 */
public class IsHappySolution {
    public static void main(String[] args) {
        IsHappySolution solution = new IsHappySolution();
        System.out.println(solution.isHappy(2));
    }

    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();

        while (true) {
            int tempVal = 0;
            while (n != 0) {
                tempVal += Math.pow(n % 10, 2);
                n /= 10;
            }
            boolean isAdd = set.add(tempVal);
            if (!isAdd) {
                return false;
            }
            if (tempVal == 1) {
                return true;
            }
            n = tempVal;
        }
    }
}
